package cn.loioi.multi.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.loioi.multi.common.BaseServiceImpl;
import cn.loioi.multi.enums.MessageState;
import cn.loioi.multi.mysql.been.SysMessageMapper;
import cn.loioi.multi.mysql.model.SysMessage;
import cn.loioi.multi.service.SysMessageService;
import tk.mybatis.mapper.common.Mapper;
import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.entity.Example.Criteria;

@Service
public class SysMessageServiceImpl extends BaseServiceImpl<SysMessage> implements SysMessageService {
	@Autowired
	private SysMessageMapper sysMessageMapper;

	@Override
	public List<SysMessage> selectByAccount(String account) {
		Example example = new Example(SysMessage.class);
		Criteria criteria = example.createCriteria();
		criteria.andEqualTo("account", account);
		criteria.andEqualTo("state", MessageState.UN_READ.getCode());
		example.orderBy("createTime").desc();
		return mapper().selectByExample(example);
	}

	@Override
	public int countForUnRead(String account) {
		Example example = new Example(SysMessage.class);
		Criteria criteria = example.createCriteria();
		criteria.andEqualTo("account", account);
		criteria.andEqualTo("state", MessageState.UN_READ.getCode());
		return mapper().selectCountByExample(example);

	}

	@Override
	public int insert(SysMessage record) {
		record.setCreateTime(new Date());
		return super.insert(record);
	}

	@Override
	public int update(SysMessage record) {
		record.setUpdateTime(new Date());
		return super.update(record);
	}

	@Override
	protected Mapper<SysMessage> mapper() {
		return sysMessageMapper;
	}

	@Override
	protected Class<SysMessage> getGenericSuperclass() {
		return SysMessage.class;
	}
}
